/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service.formula;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.formula.StFormulaNameChild;
import com.jeesite.modules.st.dao.formula.StFormulaNameDaoChild;
import com.jeesite.modules.st.entity.formula.StFormulaDetail;
import com.jeesite.modules.st.dao.formula.StFormulaDetailDao;

/**
 * 生产配方Service
 * @author HOHD
 * @version 2020-12-31
 */
@Service
@Transactional(readOnly=true)
public class StFormulaNameService extends CrudService<StFormulaNameDaoChild, StFormulaNameChild> {
	
	@Autowired
	private StFormulaDetailDao stFormulaDetailDao;
	
	/**
	 * 获取单条数据
	 * @param stFormulaName
	 * @return
	 */
	@Override
	public StFormulaNameChild get(StFormulaNameChild stFormulaName) {
		StFormulaNameChild entity = super.get(stFormulaName);
		if (entity != null){
			StFormulaDetail stFormulaDetail = new StFormulaDetail(entity);
			stFormulaDetail.setStatus(StFormulaDetail.STATUS_NORMAL);
			entity.setStFormulaDetailList(stFormulaDetailDao.findList(stFormulaDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param stFormulaName 查询条件
	 * @return
	 */
	@Override
	public Page<StFormulaNameChild> findPage(StFormulaNameChild stFormulaName) {
		return super.findPage(stFormulaName);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param stFormulaName
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(StFormulaNameChild stFormulaName) {
		super.save(stFormulaName);
		// 保存 StFormulaName子表
		for (StFormulaDetail stFormulaDetail : stFormulaName.getStFormulaDetailList()){
			if (!StFormulaDetail.STATUS_DELETE.equals(stFormulaDetail.getStatus())){
				stFormulaDetail.setFormulaNameId(stFormulaName);
				if (stFormulaDetail.getIsNewRecord()){
					stFormulaDetailDao.insert(stFormulaDetail);
				}else{
					stFormulaDetailDao.update(stFormulaDetail);
				}
			}else{
				stFormulaDetailDao.delete(stFormulaDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param stFormulaName
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(StFormulaNameChild stFormulaName) {
		super.updateStatus(stFormulaName);
	}
	
	/**
	 * 删除数据
	 * @param stFormulaName
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(StFormulaNameChild stFormulaName) {
		super.delete(stFormulaName);
		StFormulaDetail stFormulaDetail = new StFormulaDetail();
		stFormulaDetail.setFormulaNameId(stFormulaName);
		stFormulaDetailDao.deleteByEntity(stFormulaDetail);
	}
	
}